<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  $isEdit               = 0;
  $partyId              = "";
  $companyName          = "";
  $insertDateTime       = "";
  $targetedDate         = "";
  $technicianName       = "";
  $categoryId           = "";
  $categoryName         = "";
  $description          = "";
  $complainDate         = "";
  $importantPoint       = "";
  $typeOfCallId         = "";
  $type                 = "";
  $serviceCharge        = "";
  $remarks              = "";
  $amtCollectedSelected = "";
  $customerSign         = "";
  $complainCount        = 0;
  $partySelected        = "";
  $companyNameSelected  = "";
  $technicianSelected   = "";
  $categorySelected     = "";
  $ymdComplainDate     = "";
  $typeOfCallIdSelected = "";
  $deactive             = "D";
  $closeDate            = date('Y-m-d H:i:s');
  $complainServiceId    = isset($_REQUEST['complainServiceId']) ? $_REQUEST['complainServiceId'] : 0;
  $complete             = isset($_REQUEST['complete']) ? $_REQUEST['complete'] : 0;
  $inComplete           = isset($_REQUEST['inComplete']) ? $_REQUEST['inComplete'] : 0;
  $amountCollected      = isset($_REQUEST['amountCollected']) && $_REQUEST['amountCollected'] != "" ? $_REQUEST['amountCollected'] : 0;
  $updateComplainServiceEntryResult  = "";
  $print                = isset($_REQUEST['print']) ? $_REQUEST['print'] : 0;
  $printArray           = array();
  
  if(isset($_POST['insertBtn']) || isset($_POST['printBtn']))
  {
	  if(isset($_REQUEST['complainServiceId']) &&  $_REQUEST['complainServiceId'] > 0)
		{
		  if($complete == 1)
		  {
		  	$updateCmpSrvComplete = "UPDATE complainservice 
		                                SET description       = '".nl2br(addslashes($_POST['description']))."',
		                                    importantPoint    = '".nl2br(addslashes($_POST['importantPoint']))."',
		                                    typeOfCallId      = '".$_POST['typeOfCallId']."',
		                                    serviceCharge     = '".$_POST['serviceCharge']."',
		                                    remarks           = '".nl2br(addslashes($_POST['remarks']))."',
		                                    amountCollected   = '".$amountCollected."',
	                                      status            = '".$deactive."',
	                                      closeDate         = '".$closeDate."'
		                              WHERE complainServiceId = ".$_REQUEST['complainServiceId'];
		    $updateCmpSrvCompleteResult = mysql_query($updateCmpSrvComplete);  
		  	if(!$updateCmpSrvCompleteResult)                 
		      die("Update Query Error : ". mysql_error()); 
		    else
		    {                                          
		      header("Location:serviceList.php");  
		      exit();
		    }
		  }
		  elseif($inComplete == 1)
		  {
		    $updateCmpSrvInComplete = "UPDATE complainservice 
	                                    SET targetedDate      = '".$_POST['targetedDate']."',
	                                        description       = '".nl2br(addslashes($_POST['description']))."',
	                                        importantPoint    = '".nl2br(addslashes($_POST['importantPoint']))."',
	                                        typeOfCallId      = '".$_POST['typeOfCallId']."',
	                                        serviceCharge     = '".$_POST['serviceCharge']."',
	                                        remarks           = '".nl2br(addslashes($_POST['remarks']))."',
	                                        amountCollected   = '".$amountCollected."'
	                                  WHERE complainServiceId =".$_REQUEST['complainServiceId'];
	  	  $updateCmpSrvInCompleteResult = mysql_query($updateCmpSrvInComplete);   
		    if(!$updateCmpSrvInCompleteResult)                 
		      die("Update Query Error : ". mysql_error()); 
		    else     
		    {                                     
		      header("Location:serviceList.php");
		      exit();
		    }
		  }
		  else
		  {
		  	$insertDateTime       = $_POST['insertDateYear']."-".$_POST['insertDateMonth']."-".$_POST['insertDateDay'];
		  	$curDate = new DateTime();
        $curDate->setTimezone($cfgTimeZone);  //cfgTimeZone declare in config file
        $timeset = $curDate->format('H:i:s');
		    //$complainDate   = date("Y-m-d")." ".$timeset;
		    $updateComplainServiceEntry = "UPDATE complainservice 
	                                        SET partyId           = '".$_POST['partyIdEdited']."',
	                                            categoryId        = '".$_POST['categoryIdEdited']."',
	                                            complainDate      = '".$_POST['complainDate']."',
	                                            targetedDate      = '".$insertDateTime."',
	                                            description       = '".nl2br(addslashes($_POST['description']))."',
	                                            importantPoint    = '".nl2br(addslashes($_POST['importantPoint']))."',
	                                            typeOfCallId      = '".$_POST['typeOfCallId']."',
	                                            serviceCharge     = '".$_POST['serviceCharge']."',
	                                            remarks           = '".nl2br(addslashes($_POST['remarks']))."',
	                                            amountCollected   = '".$amountCollected."'
	                                      WHERE complainServiceId =".$_REQUEST['complainServiceId'];
	  	  $updateComplainServiceEntryResult = mysql_query($updateComplainServiceEntry);   
		    if(!$updateComplainServiceEntryResult)                 
		      die("Update Query Error : ". mysql_error()); 
		    else
		    {
		      header("Location:serviceRegisList.php");
		      exit();
		    }
	    }
	  }
	  else
	  {
	    if(isset($_POST['insertDateYear']))
	    {
		    $currentDate    = date('Y-m-d');
		    $insertDateTime = $_POST['insertDateYear']."-".$_POST['insertDateMonth']."-".$_POST['insertDateDay'];
		    $curDate = new DateTime();
        $curDate->setTimezone($cfgTimeZone);  //cfgTimeZone declare in config file
        $timeset = $curDate->format('H:i:s');
		    $complainDate   = date("Y-m-d")." ".$timeset;
		    $insertComplainService = "INSERT INTO complainservice (partyId,categoryId,currentDate,complainDate,targetedDate,description,importantPoint,
		                                                           typeOfCallId,serviceCharge,remarks,amountCollected)
		                                   VALUES (".$_POST['partyId'].",".$_POST['categoryId'].",'".$currentDate."','".$complainDate."',
		                                           '".$insertDateTime."','".nl2br(addslashes($_POST['description']))."','".nl2br(addslashes($_POST['importantPoint']))."','".$_POST['typeOfCallId']."',
		                                           '".$_POST['serviceCharge']."','".nl2br(addslashes($_POST['remarks']))."','".$amountCollected."')";
		    $insertComplainServiceResult = mysql_query($insertComplainService);
		    $lastInsertId = mysql_insert_id();
		    if(!$insertComplainServiceResult)
		    {
			    die("Insert Query Error : ". mysql_error());
			  }
			  else
			  {
			    if(isset($_POST['printBtn']))
			      header("Location:complainService.php?print=1&lastInsertId=".$lastInsertId);
			    else
			      header("Location:serviceList.php");
			  }
			}
	  }
  }
  
  // Selection of data while making complete or incomplete status : Starts
  if(isset($_REQUEST['complainServiceId']) && $_REQUEST['complainServiceId'] > 0 || $print == 1)
	{
	  if(isset($_REQUEST['complainServiceId']))
	    $idtoSelect = $_REQUEST['complainServiceId'];
	  else
	    $idtoSelect = $_REQUEST['lastInsertId'];
	    
	  $isEdit = 1;
	  $selectComplainEntry = "SELECT complainservice.complainServiceId,DATE_FORMAT(complainservice.complainDate,'%d-%m-%Y') AS complainDate,complainservice.complainDate As ymdComplainDate,
                                   DATE_FORMAT(complainservice.targetedDate,'%Y-%m-%d') AS targetedDate,DATE_FORMAT(complainservice.targetedDate,'%d-%m-%Y') AS tDate,complainservice.description,complainservice.importantPoint,
                                   complainservice.serviceCharge,complainservice.remarks,complainservice.amountCollected,
                                   party.partyId,party.companyName,party.address,
                                   category.categoryId,category.categoryName,typeofcall.typeOfCallId,typeofcall.type
				  						        FROM complainservice
					 						        JOIN party      ON party.partyId            = complainservice.partyId
							 				        JOIN category   ON category.categoryId      = complainservice.categoryId
						 					        JOIN typeofcall ON typeofcall.typeOfCallId  = complainservice.typeOfCallId
						 					       WHERE complainservice.complainServiceId =".$idtoSelect;
    $selectComplainEntryResult = mysql_query($selectComplainEntry);
	  while($complaneEntryRow = mysql_fetch_array($selectComplainEntryResult))
	  {
		  $complainServiceId    = $complaneEntryRow['complainServiceId'];
		  $partySelected        = $complaneEntryRow['partyId'];
		  $categorySelected     = $complaneEntryRow['categoryId'];
		  $ymdComplainDate      = $complaneEntryRow['ymdComplainDate'];
		  $complainDate         = $complaneEntryRow['complainDate'];
		  $targetedDate         = $complaneEntryRow['targetedDate'];
		  $description          = $complaneEntryRow['description'];
		  $importantPoint       = $complaneEntryRow['importantPoint'];
		  $typeOfCallIdSelected = $complaneEntryRow['typeOfCallId'];
		  $serviceCharge        = $complaneEntryRow['serviceCharge'];
		  $remarks              = $complaneEntryRow['remarks'];
		  $amtCollectedSelected = $complaneEntryRow['amountCollected'];
		  //Array Variable for Print Data : Starts
		  $printArray['companyName']     = $complaneEntryRow['companyName'];
		  $printArray['categoryName']    = $complaneEntryRow['categoryName'];
		  $printArray['address']         = $complaneEntryRow['address'];
		  $printArray['complainDate']    = $complaneEntryRow['complainDate'];
		  $printArray['tDate']           = $complaneEntryRow['tDate'];
		  $printArray['description']     = $complaneEntryRow['description'];
		  $printArray['importantPoint']  = $complaneEntryRow['importantPoint'];
		  $printArray['type']            = $complaneEntryRow['type'];
		  $printArray['serviceCharge']   = $complaneEntryRow['serviceCharge'];
		  $printArray['remarks']         = $complaneEntryRow['remarks'];
		  $printArray['amountCollected'] = $complaneEntryRow['amountCollected'];
		  //Array Variable for Print Data : Ends
	  }
  }
  // Selection of data while making complete or incomplete status : Ends
  
  ///// Select Party : Start
  $selectParty = "SELECT partyId,companyName
                   FROM party
               ORDER BY companyName";
  $selectPartyResult = mysql_query($selectParty);
  $b = 0;
  while($partyRow = mysql_fetch_array($selectPartyResult))
  {
    $partyId[$b]     = $partyRow['partyId'];
    $companyName[$b] = substr_replace($partyRow['companyName'],'...',40);
    $b++;
  }
  ///// Select Party : End
  ///// Select category : Start
  $selectCategary= "SELECT categoryId,categoryName
                      FROM category
                  ORDER BY categoryName";
  $technicianResult = mysql_query($selectCategary);
  $d = 0;
  while($categoryRow = mysql_fetch_array($technicianResult))
  {
    $categoryId[$d]   = $categoryRow['categoryId'];
    $categoryName[$d] = $categoryRow['categoryName'];
    $d++;
  }
  ///// Select category : End
  ///// Select typeOfCall : Start
  $selectTypeOfCall= "SELECT typeOfCallId,type
                        FROM typeofcall";
  $typeOfCallResult = mysql_query($selectTypeOfCall);
  $e = 0;
  while($typeOfCallRow = mysql_fetch_array($typeOfCallResult))
  {
    $typeOfCallId[$e]   = $typeOfCallRow['typeOfCallId'];
    $type[$e]           = $typeOfCallRow['type'];
    $e++;
  }
  ///// Select typeOfCall : End
  include("./bottom.php");
  $smarty->assign("complainServiceId",$complainServiceId);
  $smarty->assign("isEdit",$isEdit);
  $smarty->assign("partyId",$partyId);
  $smarty->assign("companyName",$companyName);
  $smarty->assign("technicianName",$technicianName);
  $smarty->assign("categoryId",$categoryId);
  $smarty->assign("categoryName",$categoryName);
  $smarty->assign("description",$description);
  $smarty->assign("importantPoint",$importantPoint);
  $smarty->assign("typeOfCallId",$typeOfCallId);
  $smarty->assign("typeOfCallIdSelected",$typeOfCallIdSelected);
  $smarty->assign("type",$type);
  $smarty->assign("complainDate",$complainDate);
  $smarty->assign("targetedDate",$targetedDate);
  $smarty->assign("serviceCharge",$serviceCharge);
  $smarty->assign("remarks",$remarks);
  $smarty->assign("amtCollectedSelected",$amtCollectedSelected);
  $smarty->assign("customerSign",$customerSign);
  $smarty->assign("partySelected",$partySelected);
  $smarty->assign("companyNameSelected",$companyNameSelected);
  $smarty->assign("technicianSelected",$technicianSelected);
  $smarty->assign("categorySelected",$categorySelected);
  $smarty->assign("ymdComplainDate",$ymdComplainDate);
  $smarty->assign("complete",$complete);
  $smarty->assign("inComplete",$inComplete);
  $smarty->assign("updateComplainServiceEntryResult",$updateComplainServiceEntryResult);
  $smarty->assign("print",$print);
  $smarty->assign("printArray",$printArray);
  if($print == 1)
    $smarty->display("complainServicePrint.tpl");
  else
    $smarty->display("complainService.tpl");
}
?>